package com.bjqh.edu;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.EasyActivity;
import com.bjqh.edu.Bean.Room;
import com.bjqh.edu.Bean.User;
import com.bjqh.edu.UI.Activity.Index.PrivateClassActivity;
import com.bjqh.edu.UI.Activity.Login.LoginActivity;
import com.bjqh.edu.UI.Activity.StudentPersonal.MySettingActivity;
import com.bjqh.edu.UI.Fragment.MainFragment;
import com.bjqh.edu.UI.Fragment.MineFragment;
import com.bjqh.edu.UI.Fragment.OneOnOneFragment;
import com.bjqh.edu.adapter.PendingAdapter;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class MainActivity extends EasyActivity implements View.OnClickListener {


    @BindView(R.id.fl_container)
    FrameLayout flContainer;
    @BindView(R.id.iv_index)
    ImageView ivIndex;
    @BindView(R.id.tv_index)
    TextView tvIndex;
    @BindView(R.id.ll_index)
    LinearLayout llIndex;
    @BindView(R.id.iv_one_on_one)
    ImageView ivOneOnOne;
    @BindView(R.id.tv_one_on_one)
    TextView tvOneOnOne;
    @BindView(R.id.ll_one_on_one)
    LinearLayout llOneOnOne;
    @BindView(R.id.iv_my)
    ImageView ivMy;
    @BindView(R.id.tv_my)
    TextView tvMy;
    @BindView(R.id.ll_my)
    LinearLayout llMy;
    @BindView(R.id.li_lefttab)
    LinearLayout liLefttab;
    @BindView(R.id.li_bottab)
    LinearLayout liBottab;
    private FragmentManager mFragmentManager;
    private int index;
    private Fragment[] fragments;
    public MainFragment homefragment;
    private OneOnOneFragment oneOnOneFragment;
    private MineFragment mineFragment;
    private ImageView[] imagebuttons = new ImageView[3];
    private TextView[] textviews = new TextView[3];
    private int currentTabIndex;// 当前fragment的index
    public static Activity mMainActivity = null;
    User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mMainActivity = this;
        initViews();
        initData();
        initEvents();
    }


    @Override
    protected void initViews() {
        llIndex.setOnClickListener(this);
        llOneOnOne.setOnClickListener(this);
        llMy.setOnClickListener(this);
        imagebuttons[0] = ivIndex;
        imagebuttons[1] = ivOneOnOne;
        imagebuttons[2] = ivMy;
        imagebuttons[0].setSelected(true);
        textviews[0] = tvIndex;
        textviews[1] = tvOneOnOne;
        textviews[2] = tvMy;
        textviews[0].setTextColor(0xFFFAB2A6);
        ivIndex.setBackgroundResource(R.mipmap.nav_sy_pre);
        ivMy.setBackgroundResource(R.mipmap.nav_wd_pre);
        ivOneOnOne.setBackgroundResource(R.mipmap.nav_yvy);
        homefragment = new MainFragment();
        oneOnOneFragment = new OneOnOneFragment();
        mineFragment = new MineFragment();
        fragments = new Fragment[]{homefragment, oneOnOneFragment,
                mineFragment};
        // 添加显示第一个fragment
        mFragmentManager=getSupportFragmentManager();
        mFragmentManager.beginTransaction()
                .add(R.id.fl_container, homefragment)
                .add(R.id.fl_container, oneOnOneFragment)
                .add(R.id.fl_container, mineFragment)
                .hide(oneOnOneFragment)
                .hide(mineFragment).show(homefragment).commit();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_index:
                index = 0;
               ivIndex.setBackgroundResource(R.mipmap.nav_sy_pre);
              ivOneOnOne.setBackgroundResource(R.mipmap.nav_yvy);
              ivMy.setBackgroundResource((R.mipmap.nav_wd_pre));
                break;
            case R.id.ll_one_on_one:
                index = 1;
                ivIndex.setBackgroundResource(R.mipmap.nav_sy);
                ivOneOnOne.setBackgroundResource(R.mipmap.nav_yvy_pre);
                ivMy.setBackgroundResource(R.mipmap.nav_wd_pre);
                OkGo.post(AppConstants.BASE_URL + "/rtu/myRoom.shtml")
                        .tag(this)
                        .cacheKey("cacheKey")
                        .cacheMode(CacheMode.DEFAULT)
                        .params("status", 4)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                Log.i("待上课程", s);
                                Gson gson = new Gson();
                                Room room = gson.fromJson(s, Room.class);
                                if (room.getMsg() != null) {
                                    if (room.getMsg().equals("timeout")) {
                                        AppConstants.gotoLogin(MainActivity.this);
                                    }
                                    else if(room.getMsg().equals("remote login"))
                                    {
                                        AppConstants.remologin(MainActivity.this);
                                    }
                                } else {
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                Toast.makeText(MainActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                            }
                        });

                break;
            case R.id.ll_my:
                index = 2;
                ivIndex.setBackgroundResource(R.mipmap.nav_sy);
                ivOneOnOne.setBackgroundResource(R.mipmap.nav_yvy);
               ivMy.setBackgroundResource(R.mipmap.nav_wd);
                break;
            default:

        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = mFragmentManager
                    .beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fl_container, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        imagebuttons[currentTabIndex].setSelected(false);
        // 把当前tab设为选中状态
        imagebuttons[index].setSelected(true);
        textviews[index].setTextColor(0xFFFAB2A6);
        textviews[currentTabIndex].setTextColor(0xFF989898);
        currentTabIndex = index;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
       // AppConstants.keyDown(MainActivity.this);
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }
}